#include <stdio.h>
#include <sys/epoll.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <pthread.h>

/**
 * @param fd [设置套接字为非阻塞]
 */
void set_nonblocking(int fd) {
	int flag = fcntl(fd, F_GETFL, 0);
	if ( -1 == flag ) {
		return;
	}

	fcntl(fd, F_SETFL, flag | O_NONBLOCK);
}


void event_add(int efd, int fd, int events)
{
	struct epoll_event ev;
	ev.events = events;
	ev.data.fd = fd;
	
	epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev);
	
	set_nonblocking(fd);
}

void event_del(int efd, int fd)
{
	struct epoll_event ev;
	ev.data.fd = fd;
	
	epoll_ctl(efd, EPOLL_CTL_DEL, fd, NULL);
}

void event_write(int efd, int sock, int events) 
{
	struct epoll_event ev;
	ev.events = events;
	epoll_ctl(efd, EPOLL_CTL_MOD, sock, &ev);
}

int event_create()
{
	return epoll_create(1024);
}

int event_wait(int efd, struct epoll_event ev[], int max) {
	//struct epoll_event ev[max];
	int n = epoll_wait(efd , ev, max, -1);

	return n;
}

